Skip to content

Simple enhancement for pattern matching with capturing types #23524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2025

Conversation

noti0na1
Copy link
Member

The bind variable should carry the capture set from the selector during pattern matching.

import language.experimental.captureChecking

trait A

case class B(x: AnyRef^) extends A

def test =
  val x: AnyRef^ = new AnyRef
  val a: A^{x} = B(x)

  val y1: A = a match
    case b: B => b // error: (b: B) becomes B^{x} implicitly

  val y2: A^{x} = a match
    case b: B => b // ok

  val x3: AnyRef = a match
    case B(x2: AnyRef) => x2 // error: we lose some information about field x, but it still cannot be pure

  val x4: AnyRef = a match
    case b: B => b.x // error

@noti0na1 noti0na1 requested a review from Copilot July 14, 2025 14:31
Copilot

This comment was marked as resolved.

@noti0na1 noti0na1 marked this pull request as ready for review July 14, 2025 18:40
@noti0na1 noti0na1 force-pushed the cc-enhance-match branch 2 times, most recently from 3f16d46 to e0256bd Compare August 15, 2025 11:35
@noti0na1 noti0na1 requested a review from bracevac August 15, 2025 12:18
@bracevac
Copy link
Contributor

In this case:

val y2: A^{x} = a match
    case b: B => b // ok

Can we conclude that b: B^{b} and B^{b} <: A^{x} ?

@noti0na1
Copy link
Member Author

In this case:

val y2: A^{x} = a match
    case b: B => b // ok

Can we conclude that b: B^{b} and B^{b} <: A^{x} ?

Yes, they work

@noti0na1 noti0na1 merged commit 3349444 into scala:main Aug 16, 2025
48 checks passed
@noti0na1 noti0na1 deleted the cc-enhance-match branch August 16, 2025 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants